<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="title">{{$t('achievements.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 名称 -->
          <div class="searchItem">
            <span>{{$t('group.name')}}</span>
            <el-input
              v-model="listQuery.ruleName"
              size="small"
              :placeholder="$t('MessagePush.inpMessageRule')"
            ></el-input>
          </div>
          <div class="menu-title-btn" style="margin-left: 20px;">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div style="display: flex; justify-content: space-between;">
        <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
        <div>
          <el-button
            type="primary"
            size="small"
            @click="sendNoticeFn"
          >{{$t('MessagePush.sendTimeNotice')}}</el-button>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%; margin-top: 5px;"
        border
        size="mini"
      >
        <el-table-column :label="$t('user.num')" header-align="center" align="center" width="60">
          <template slot-scope="scope">
            <span>{{scope.row.index}}</span>
          </template>
        </el-table-column>
        <!-- 推送规则 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="ruleName"
          :label="$t('MessagePush.PushRules')"
        ></el-table-column>
        <!-- 状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="status"
          width="160"
          :label="$t('grouping.status')"
        >
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.status" size="small" @change.native="changeStatus(scope.row)">
                <el-radio label="1" size="mini">{{$t("AppVersion.enable")}}</el-radio>
                <el-radio label="2" size="mini">{{$t("AppVersion.blockup")}}</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column align="center" header-align="center"  :label="$t('user.operation')" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleUpdate(scope.row)">{{$t('btn.edit')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="$t('MessagePush.sendTimeNotice')"
      :visible.sync="dialogSendFormVisible"
      :before-close="closeDialog"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <!-- 公告标题 -->
        <el-form-item :label="$t('MessagePush.NoticeTitle')" prop="msgTitle">
          <el-input size="small" v-model="form.msgTitle" style="width: 92%;" maxlength="15"></el-input>
        </el-form-item>
        <!-- 公告内容 -->
        <el-form-item :label="$t('MessagePush.NoticeContent')" prop="msgContent">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 5}"
            v-model="form.msgContent"
            style="width: 92%;"
          ></el-input>
        </el-form-item>
        <!-- 用户群体 -->
        <el-form-item :label="$t('MessagePush.userGroup')" prop="userType">
          <el-select v-model="form.userType" size="small">
            <el-option
              v-for="item in userGroupArr"
              :key="item.code"
              :label="item.chsMsg"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sendNoticeSave('form')">{{$t('approvalDetail.send')}}</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="$t('MessagePush.editMsg')"
      :visible.sync="dialogEditFormVisible"
      :before-close="closeDialogEdit"
    >
      <el-form :model="editform" :rules="editRules" ref="editform" label-width="140px">
        <!-- 消息标题 -->
        <el-form-item :label="$t('MessagePush.msgTitle')" prop="msgTitle">
          <el-input size="small" v-model="editform.msgTitle" style="width: 92%;" :maxlength="15"></el-input>
        </el-form-item>
        <!-- 消息内容 -->
        <el-form-item :label="$t('MessagePush.msgContent')" prop="msgContent" :maxlength="30">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 5}"
            v-model="editform.msgContent"
            style="width: 92%;"
          ></el-input>
        </el-form-item>
        <!-- 每日发送时间 -->
        <el-form-item :label="$t('MessagePush.DailySendtime')" required>
          <el-col :span="9">
            <el-form-item prop="sendHour">
              <el-input
                size="small"
                :placeholder="$t('MessagePush.inpTime')"
                v-model="editform.sendHour"
                style="width: 100%;"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1" style="text-align: center;">：</el-col>
          <el-col :span="9">
            <el-form-item prop="sendMinute">
              <el-input
                size="small"
                :placeholder="$t('MessagePush.inpBranch')"
                v-model="editform.sendMinute"
                style="width: 100%;"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <!-- 每日二次发送时间 -->
        <el-form-item :label="$t('MessagePush.TwoSendtime')">
          <el-col :span="9">
            <el-form-item prop="sendSecondHour">
              <el-input
                size="small"
                :placeholder="$t('MessagePush.inpTime')"
                v-model="editform.sendSecondHour"
                style="width: 100%;"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1" style="text-align: center;">：</el-col>
          <el-col :span="9">
            <el-form-item prop="sendSecondMinute">
              <el-input
                size="small"
                :placeholder="$t('MessagePush.inpBranch')"
                v-model="editform.sendSecondMinute"
                style="width: 100%;"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSave('editform')">{{$t('btn.conserve')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  page,
  updateMessageRuleStatus,
  updateMessageRule,
  sendLiveAnnouncements,
  getUserGroup
} from "api/synManager/MessagePush/index";
export default {
  data() {
    return {
      editform: {
        msgTitle: undefined,
        msgContent: undefined,
        sendHour: undefined,
        sendMinute: undefined,
        sendSecondHour: undefined,
        sendSecondMinute: undefined
      },
      editRules: {
        msgTitle: [
          {
            required: true,
            message: this.$t("MessagePush.ruleMsgTitle")
          }
        ],
        msgContent: [
          {
            required: true,
            message: this.$t("MessagePush.ruleMsgContent")
          }
        ],
        sendHour: [
          {
            required: true,
            message: this.$t("MessagePush.ruleDailySendtime1")
          }
        ],
        sendMinute: [
          {
            required: true,
            message: this.$t("MessagePush.ruleDailySendtime2")
          }
        ]
      },
      form: {
        msgTitle: undefined,
        msgContent: undefined,
        userType: undefined
      },
      rules: {
        msgTitle: [
          {
            required: true,
            message: this.$t("MessagePush.ruleNoticeTitle")
          }
        ],
        msgContent: [
          {
            required: true,
            message: this.$t("MessagePush.ruleNoticeContent")
          }
        ],
        userType: [
          {
            required: true,
            message: this.$t("MessagePush.ruleUserGroup")
          }
        ]
      },
      dialogSendFormVisible: false,
      dialogEditFormVisible: false,
      listLoading: false,
      total: null,
      listQuery: {
        limit: 10,
        page: 1,
        ruleName: ""
      },
      tableData: [
        {
          ruleName: "",
          status: "1"
        }
      ],
      userGroupArr: [],
      currentId: ""
    };
  },
  computed: {
    ...mapGetters(["elements", "MessagePushPage"])
  },
  created() {
    if (this.MessagePushPage.has(this.$route.path)) {
      this.listQuery = this.MessagePushPage.get(this.$route.path);
    }
    this.getList();
  },
  methods: {
    closeDialog(done) {
      done();
      this.$refs["form"].resetFields();
      this.getList();
    },
    // 发送实时公告
    sendNoticeFn() {
      this.dialogSendFormVisible = true;
      // 获取用户群体
      getUserGroup().then(res => {
        this.userGroupArr = res.data;
      });
    },
    // 发送实时公告发送按钮
    sendNoticeSave(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning",
            center: true
          })
            .then(() => {
              sendLiveAnnouncements(this.form).then(res => {
                if (res.status === 200) {
                  this.dialogSendFormVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t("user.success"),
                    message: this.$t("loanDetail.cue2"),
                    type: "success",
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: res.message
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                showClose: true,
                type: "info",
                message: this.$t("loanDetail.cue3")
              });
            });
        } else {
          return false;
        }
      });
    },
    closeDialogEdit(done) {
      done();
      this.$refs["editform"].resetFields();
      this.getList();
    },
    // 点击编辑按钮
    handleUpdate(row) {
      this.editform = Object.assign({}, row);
      this.currentId = row.id;
      this.dialogEditFormVisible = true;
    },
    // 编辑保存按钮
    editSave(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t("loanDetail.cue1"), {
            confirmButtonText: this.$t("btn.confirm"),
            cancelButtonText: this.$t("btn.cancel"),
            type: "warning",
            center: true
          })
            .then(() => {
              this.editform.id = this.currentId;
              updateMessageRule(this.editform).then(res => {
                if (res.status === 200) {
                  this.dialogEditFormVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t("user.success"),
                    message: this.$t("wordbook.wbeditsuc"),
                    type: "success",
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: res.message
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                showClose: true,
                type: "info",
                message: this.$t("loanDetail.cue3")
              });
            });
        } else {
          return false;
        }
      });
    },
    // 修改状态
    changeStatus(row) {
      let obj = {};
      obj.ruleId = row.id;
      obj.status = row.status;
      updateMessageRuleStatus(obj).then(res => {
        if (res.status === 200) {
          this.$message({
            showClose: true,
            type: "success",
            message: this.$t("approvalDispatch.opersuccess")
          });
          this.getList();
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
          this.getList();
        }
      });
    },
    getList() {
      // this.listLoading = true;
      // this.$store.dispatch("saveMessagePushPage", {
      //   path: this.$route.path,
      //   listQuery: this.listQuery
      // });
      // page(this.listQuery).then(res => {
      //   res.data.records.forEach((item, index) => {
      //     item.index =
      //       index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
      //   });
      //   this.listLoading = false;
      //   this.tableData = res.data.records;
      //   this.total = Number(res.data.total);
      // });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 200px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
</style>